package com.qf.action;

import com.qf.dto.HouseDTO;
import com.qf.dto.PageDTO;
import com.qf.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

@Controller
public class HouseAction {
    /*房子的表现层*/
    @Autowired
    private HouseService houseService;
    /*添加功能*/
    @RequestMapping("/addHouse.do")
    public ModelAndView addHouse(HttpServletRequest request, HouseDTO dto){
        ModelAndView modelAndView = new ModelAndView();
        int i = 0;
        try {
            i = houseService.addHouse(dto);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (i>0){
            modelAndView.setViewName("forward:/houList.do");
        }else {
            modelAndView.setViewName("error");
        }
        return modelAndView;
    }
    //分页展示功能
    @RequestMapping("/houList.do")
    public ModelAndView findHouByPage(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        String page = request.getParameter("page");
        int currentPage=1;
        if (StringUtils.isEmpty(page)){
            currentPage=1;
        }else {
            currentPage=Integer.parseInt(page);
        }
        if (currentPage<=1){
            currentPage=1;
        }
        PageDTO pageDTO = new PageDTO();
        pageDTO.setCurrentPage(currentPage);
        List<HouseDTO> dots = houseService.findHouByPage(pageDTO);
        int maxPage = pageDTO.getMaxPage();
        if (currentPage>=maxPage){
            currentPage = maxPage;
        }
        modelAndView.addObject("houList",dots);
        modelAndView.addObject("currentPage",currentPage);
        modelAndView.addObject("maxPage",maxPage);
        modelAndView.setViewName("list");
        return modelAndView;
    }
    //删除功能
    @RequestMapping("/delHou.do")
    public String delHou(HttpServletRequest request,Long[]checkOne){
        for (Long id : checkOne) {
            int i = houseService.delHou(id);
        }
        return "forward:/houList.do";
    }

    //详情展示功能
    @RequestMapping("/detail.do")
    public ModelAndView detail(HttpServletRequest request, Long id){
        ModelAndView modelAndView = new ModelAndView();
        HouseDTO dto = houseService.findHouById(id);
        //存到session中，以便后面的编辑使用
        request.getSession().setAttribute("houseDTO",dto);
        modelAndView.addObject("houseDTO",dto);
        modelAndView.setViewName("detail");
        return modelAndView;
    }
    @RequestMapping("/detail2.do")
    public ModelAndView detail2(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        HouseDTO houseDTO = (HouseDTO) request.getSession().getAttribute("houseDTO");
        modelAndView.addObject("houseDTO",houseDTO);
        modelAndView.setViewName("edit");
        return modelAndView;
    }

    //修改功能
    @RequestMapping("/edit2.do")
    public String edit(HttpServletRequest request,HouseDTO dto){
        dto.setId(Long.parseLong(request.getParameter("id")));
        System.out.println(dto+"==============================================");
        try {
            houseService.updateHou(dto);
        } catch (ParseException e) {
            System.out.println("更新失败！！！！！！");
            e.printStackTrace();
        }
        return "forward:/houList.do";
    }
}
